import 'package:flutter/material.dart';
import 'package:silver_flutter/bookshelf_page.dart';
import 'package:silver_flutter/categories_page.dart';
import 'package:silver_flutter/home_page.dart';
import 'package:silver_flutter/updates_page.dart';
import 'package:silver_flutter/user_page.dart';

void main() => runApp(new App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = '吃吃漫画';

    return new MaterialApp(
      title: title,
      theme: new ThemeData(
        primarySwatch: Colors.pink,
      ),
      home: new MainNavigator(title: title),
    );
  }
}

class MainNavigator extends StatefulWidget {
  MainNavigator({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MainNavigatorState createState() => new _MainNavigatorState();
}

class _MainNavigatorState extends State<MainNavigator> {
  int _currentIndex = 2;
  List<Widget> _pages;

  @override
  void initState() {
    super.initState();

    _pages = <Widget>[
      new UpdatesPage(),
      new CategoriesPage(),
      new HomePage(title: widget.title),
      new BookshelfPage(),
      new UserPage(),
    ];
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return new Scaffold(
      body: new Stack(
        children: new List<Widget>.generate(
            _pages.length,
            (i) => new Offstage(
                  offstage: _currentIndex != i,
                  child: _pages[i],
                )),
      ),
      bottomNavigationBar: new BottomNavigationBar(
        items: [
          new BottomNavigationBarItem(
            icon: new Icon(Icons.update),
            title: new Text('更新'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.category),
            title: new Text('分类'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.home),
            title: new Text('推荐'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.library_books),
            title: new Text('书架'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.person),
            title: new Text('我的'),
          ),
        ],
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            this._currentIndex = index;
          });
        },
      ),
    );
  }
}
